# Copyright © 2024 Daniel Semyonov <daniel@dsemy.com>

# Permission is hereby granted, free of charge, to any person obtaining a
# copy of this software and associated documentation files (the "Software"),
# to deal in the Software without restriction, including without limitation
# the rights to use, copy, modify, merge, publish, distribute, sublicense,
# and/or sell copies of the Software, and to permit persons to whom the
# Software is furnished to do so, subject to the following conditions:

# The above copyright notice and this permission notice (including the next
# paragraph) shall be included in all copies or substantial portions of the
# Software.

# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
# DEALINGS IN THE SOFTWARE.

PREFIX ?= /usr/local
MAN_PREFIX ?= $(PREFIX)/share/man
CFLAGS ?= -std=c99 -Wall -Wextra -Wno-unused-parameter -Wno-strict-prototypes -Wno-incompatible-pointer-types
PKG_CONFIG ?= pkg-config
INSTALL ?= install

WAYLAND_FLAGS = $(shell $(PKG_CONFIG) --cflags --libs wayland-client)
WAYLAND_SCANNER = $(shell $(PKG_CONFIG) --variable=wayland_scanner wayland-scanner)

all: wlr-dpms

wlr-dpms: wlr-dpms.c wlr-output-power-management-unstable-v1-protocol.c
	$(CC) $(CFLAGS) -o $@ $^ $(WAYLAND_FLAGS)

wlr-output-power-management-unstable-v1-client-protocol.h: wlr-output-power-management-unstable-v1.xml
	$(WAYLAND_SCANNER) client-header $< $@

wlr-output-power-management-unstable-v1-protocol.c: wlr-output-power-management-unstable-v1.xml wlr-output-power-management-unstable-v1-client-protocol.h
	$(WAYLAND_SCANNER) private-code $< $@

.PHONY: install clean

install: wlr-dpms
	$(INSTALL) -d $(DESTDIR)$(PREFIX)/bin
	$(INSTALL) -d $(DESTDIR)$(MAN_PREFIX)/man1
	$(INSTALL) -m 755 wlr-dpms $(DESTDIR)$(PREFIX)/bin
	$(INSTALL) -m 644 wlr-dpms.1 $(DESTDIR)$(MAN_PREFIX)/man1

clean:
	$(RM) wlr-dpms *.h wlr-output-power-management-unstable-v1-protocol.c
